﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;

namespace LED_Tester
{
    public class ClassSourceMeter
    {
        public int Join(int mode, SerialPort sptSourceMeter, string portName, int baudRate)
        {
            int ret = -1;

            #region 外部

            if (mode == 0)
            {
                ret = 0;
            }

            #endregion

            #region IT6133B

            else if (mode == 1)
            {
                sptSourceMeter.BaudRate = baudRate;
                sptSourceMeter.DataBits = 8;
                sptSourceMeter.StopBits = StopBits.One;
                sptSourceMeter.Parity = Parity.None;
                sptSourceMeter.ReadTimeout = 300;

                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.Close();
                }

                try
                {
                    sptSourceMeter.PortName = portName;
                    sptSourceMeter.Open();
                }
                catch { }

                string recv = "";

                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.DiscardInBuffer();
                    sptSourceMeter.WriteLine("SYST:REM");
                    sptSourceMeter.WriteLine("*IDN?");

                    try
                    {
                        recv = sptSourceMeter.ReadLine();
                    }
                    catch { }

                    if (recv.Length < 14)
                    {
                        ret = 1;
                    }
                    else
                    {
                        if (recv.Substring(7, 7) == "IT6133B")
                        {
                            ret = 0;
                        }
                        else
                        {
                            ret = 1;
                        }
                    }
                }
                else
                {
                    ret = 1;
                }
            }

            #endregion

            #region KY2425

            else if (mode == 2)
            {
                sptSourceMeter.BaudRate = baudRate;
                sptSourceMeter.DataBits = 8;
                sptSourceMeter.StopBits = StopBits.One;
                sptSourceMeter.Parity = Parity.None;
                sptSourceMeter.ReadTimeout = 300;

                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.Close();
                }

                try
                {
                    sptSourceMeter.PortName = portName;
                    sptSourceMeter.Open();
                }
                catch { }

                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.DiscardInBuffer();
                    sptSourceMeter.WriteLine("*IDN?");

                    string recv = "";

                    try
                    {
                        recv = sptSourceMeter.ReadLine();
                    }
                    catch { }

                    if (recv.Length < 84)
                    {
                        ret = 1;
                    }
                    else
                    {
                        string temp = recv.Substring(1, recv.Length - 3);

                        if (temp.Substring(32, 4) == "2425")
                        {
                            ret = 0;
                        }
                        else
                        {
                            ret = 1;
                        }
                    }
                }
                else
                {
                    ret = 1;
                }
            }

            #endregion

            return ret;
        }

        public void Set(int mode, SerialPort sptSourceMeter, double voltage, double current)
        {
            if (mode == 0)
            {
                return;
            }
            else if (mode == 1)
            {
                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.DiscardInBuffer();
                    sptSourceMeter.WriteLine("VOLT " + voltage.ToString("#0.0000") + "V;:CURR " + current.ToString("#0.0000") + "A");
                }
            }
            else if (mode == 2)
            {
                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.WriteLine(":SOUR:FUNC CURR;:SOUR:CURR:RANG:AUTO ON;:SENS:VOLT:RANG:AUTO ON");
                    sptSourceMeter.WriteLine(":SENS:VOLT:PROT " + voltage.ToString() + ";:SOUR:CURR " + current.ToString());
                    sptSourceMeter.WriteLine(":SENS:FUNC " + "'VOLT'");
                }
            }
        }

        public void Measure(int mode, SerialPort sptSourceMeter, ref double voltage, ref double current)
        {
            if (mode == 0)
            {
                voltage = 0;
                current = 0;
            }
            else if (mode == 1)
            {
                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.DiscardInBuffer();
                    sptSourceMeter.WriteLine("MEAS:DVM?;:MEAS:CURR?");

                    string recv = "";

                    try
                    {
                        recv = sptSourceMeter.ReadLine();
                    }
                    catch { }

                    if (recv.Length > 8)
                    {
                        string[] temp = recv.Split(new char[] { ';' });
                        voltage = double.Parse(temp[0].Trim());
                        current = double.Parse(temp[1].Trim());
                    }
                    else
                    {
                        voltage = 0;
                        current = 0;
                    }
                }
            }
            else if (mode == 2)
            {
                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.DiscardInBuffer();
                    sptSourceMeter.WriteLine(":READ?");

                    string recv = "";

                    try
                    {
                        recv = sptSourceMeter.ReadLine();
                    }
                    catch { }

                    if (recv.Length > 8)
                    {
                        string temp = recv.Substring(1, recv.Length - 3);
                        string[] temps = temp.Split(new char[] { ',' });

                        voltage = double.Parse(temps[0].Trim());
                        current = double.Parse(temps[1].Trim());
                    }
                    else
                    {
                        voltage = 0;
                        current = 0;
                    }
                }
            }
        }

        public void LightOn(int mode, SerialPort sptSourceMeter)
        {
            if (mode == 0)
            {
                return;
            }
            else if (mode == 1)
            {
                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.DiscardInBuffer();
                    sptSourceMeter.WriteLine("OUTP 1");
                }
            }
            else if (mode == 2)
            {
                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.DiscardInBuffer();
                    sptSourceMeter.WriteLine(":OUTP:STAT ON");
                }
            }
        }

        public void LightOff(int mode, SerialPort sptSourceMeter)
        {
            if (mode == 0)
            {
                return;
            }
            else if (mode == 1)
            {
                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.DiscardInBuffer();
                    sptSourceMeter.WriteLine("OUTP 0");
                }
            }
            else if (mode == 2)
            {
                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.DiscardInBuffer();
                    sptSourceMeter.WriteLine(":OUTP:STAT OFF");
                }
            }
        }

        public void Eixt(int mode, SerialPort sptSourceMeter)
        {
            if (mode == 0)
            {
                return;
            }
            else if (mode == 1)
            {
                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.DiscardInBuffer();
                    sptSourceMeter.WriteLine("SYST:LOC");
                    Thread.Sleep(100);
                    sptSourceMeter.Close();
                }
            }
            else if (mode == 2)
            {
                if (sptSourceMeter.IsOpen)
                {
                    sptSourceMeter.Close();
                }
            }
        }
    }
}
